class NumMatrix(object):
    def __init__(self, matrix):
        """
        :type matrix: List[List[int]]
        """
        m = len(matrix)
        if not m:
            return
        n = len(matrix[0])
        data = [[0] * (n) for _ in range(m)]
        data[0][0] = matrix[0][0]
        for i in range(1, m):
            data[i][0] = data[i - 1][0] + matrix[i][0]

        for j in range(1, n):
            data[0][j] = data[0][j - 1] + matrix[0][j]

        for i in range(1, m):
            for j in range(1, n):
                data[i][j] = (
                    data[i - 1][j] + data[i][j - 1] - data[i - 1][j - 1] + matrix[i][j]
                )
                if i == 1 and j == 1:
                    print(data)
                    print(matrix)
                    print(data[i - 1][j] + data[i][j - 1] + matrix[i][j] - matrix[0][0])

        self.data = data
        print(data)

    def sumRegion(self, row1, col1, row2, col2):
        """
        :type row1: int
        :type col1: int
        :type row2: int
        :type col2: int
        :rtype: int
        """
        # return (
        #     self.data[row2][col2]
        #     - self.data[row2][col1 - 1]
        #     - self.data[row1 - 1][col2]
        #     + self.data[row1 - 1][col1 - 1]
        # )

        res = self.data[row2][col2]
        # if row1 == 0 and col1 == 0:
        # return res

        if row1 > 0:
            res -= self.data[row1 - 1][col2]
        if col1 > 0:
            res -= self.data[row2][col1 - 1]
        if row1 > 0 and col1 > 0:
            res += self.data[row1 - 1][col1 - 1]
        return res


if __name__ == "__main__":
    matrix = [
        [3, 0, 1, 4, 2],
        [5, 6, 3, 2, 1],
        [1, 2, 0, 1, 5],
        [4, 1, 0, 1, 7],
        [1, 0, 3, 0, 5],
    ]

    obj = NumMatrix(matrix)


# Your NumMatrix object will be instantiated and called as such:
# obj = NumMatrix(matrix)
# param_1 = obj.sumRegion(row1,col1,row2,col2)
